package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.IUserBlacklistService;
import com.mdd.admin.validate.UserBlacklistCreateValidate;
import com.mdd.admin.validate.UserBlacklistUpdateValidate;
import com.mdd.admin.validate.UserBlacklistSearchValidate;
import com.mdd.admin.vo.UserBlacklistListedVo;
import com.mdd.admin.vo.UserBlacklistDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.UserBlacklist;
import com.mdd.common.mapper.UserBlacklistMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 黑名单实现类
 * @author DEREK
 */
@Service
public class UserBlacklistServiceImpl implements IUserBlacklistService {
        
    @Resource
    UserBlacklistMapper userBlacklistMapper;

    /**
     * 黑名单列表
     *
     * @author DEREK
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<UserBlacklistListedVo>
     */
    @Override
    public PageResult<UserBlacklistListedVo> list(PageValidate pageValidate, UserBlacklistSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<UserBlacklist> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        userBlacklistMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:userId@user_id:int",
            "=:toUserId@to_user_id:int",
        });

        IPage<UserBlacklist> iPage = userBlacklistMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<UserBlacklistListedVo> list = new LinkedList<>();
        for(UserBlacklist item : iPage.getRecords()) {
            UserBlacklistListedVo vo = new UserBlacklistListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 黑名单详情
     *
     * @author DEREK
     * @param id 主键参数
     * @return UserBlacklist
     */
    @Override
    public UserBlacklistDetailVo detail(Integer id) {
        UserBlacklist model = userBlacklistMapper.selectOne(
                new QueryWrapper<UserBlacklist>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        UserBlacklistDetailVo vo = new UserBlacklistDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 黑名单新增
     *
     * @author DEREK
     * @param createValidate 参数
     */
    @Override
    public void add(UserBlacklistCreateValidate createValidate) {
        UserBlacklist model = new UserBlacklist();
        model.setUserId(createValidate.getUserId());
        model.setToUserId(createValidate.getToUserId());
        model.setCreateTime(System.currentTimeMillis() / 1000);
        userBlacklistMapper.insert(model);
    }

    /**
     * 黑名单编辑
     *
     * @author DEREK
     * @param updateValidate 参数
     */
    @Override
    public void edit(UserBlacklistUpdateValidate updateValidate) {
        UserBlacklist model = userBlacklistMapper.selectOne(
                new QueryWrapper<UserBlacklist>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setUserId(updateValidate.getUserId());
        model.setToUserId(updateValidate.getToUserId());
        userBlacklistMapper.updateById(model);
    }

    /**
     * 黑名单删除
     *
     * @author DEREK
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        UserBlacklist model = userBlacklistMapper.selectOne(
                new QueryWrapper<UserBlacklist>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        userBlacklistMapper.delete(new QueryWrapper<UserBlacklist>().eq("id", id));
    }

}
